<template>
  <basic-container>
    <dataList placeholder="请输入单位名称" :pageInfo="pager" :listData="tableData" searchName="unitName" :menuList="[]" :filterList="filterList">
      <el-table :data="tableData" align="center">
        <el-table-column label="授权编号" prop="authorizationNumber"></el-table-column>
        <el-table-column label="申办单位" prop="unitName"></el-table-column>
        <el-table-column label="授权物业" prop="authorizationProperty" show-overflow-tooltip></el-table-column>
        <el-table-column label="授权日期" prop="authorizationTime"></el-table-column>
        <el-table-column label="授权状态" prop="authorizationStatus"></el-table-column>
        <el-table-column label="超期天数" align="center" prop="overdueTm"></el-table-column>
        <el-table-column label="年审状态" prop="annualAuditStatus" :formatter="dataFormatter"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="isShow" type="text" size="small" @click="toEditData(scope.row)">年审</el-button>
          </template>
        </el-table-column>
      </el-table>
    </dataList>
    <edit-annual ref="editAnnual" @rest="resetList"></edit-annual>
  </basic-container>
</template>

<script>
import editAnnual from "../edit/edit-annual";
import { fetchList, getTemplate } from "../api/annualauditinfo";
import moment from "moment";
export default {
  components: { editAnnual },
  provide () {
    return {
      initList: this.queryData
    };
  },
  mounted () {
    console.log(this.annual_audit_status)
    this.queryData();
  },
  data () {
    return {
      // 表格列表数据
      tableData: [],
      // 请求参数
      params: {
        current: 1,
        size: 10
      },
      // 分页
      pager: {
        total: 0, // 总页数
        current: 1, // 当前页数
        size: 10 // 每页显示多少条
      },
      // 页面loading
      loading: false,
      // 筛选项
      filterList: [
        {
          label: "授权状态",
          inputType: "select",
          name: "authorizationStatus",
          children: [
            {
              label: "未发放",
              value: "未发放"
            },
            {
              label: "已发放",
              value: "已发放"
            }
          ]
        },
        {
          label: "授权日期",
          inputType: "date",
          name: "authorizationTm"
        }
      ],
      map: [],
      searchForm: {},
      template: {},
    };
  },
  computed: {
    // 年审固定时间
    annualDate () {
      return this.getDict("annual_date");
    },
    // 年审状态
    annualAuditStatus () {
      return this.getDict('annual_audit_status')
    },
    // 是否显示年审按钮
    isShow () {
      let date1 = new Date();
      let date2 = new Date(
        date1.getFullYear(),
        this.annualDate[0].value,
        this.annualDate[1].value
      );
      if (this.template.conditionTm) {
        return this.getDaysBetween(date1, date2) <= this.template.conditionTm
      } else if (this.template.fixedStartTm && this.template.fixedEndTm) {
        return moment(date1).isBetween(this.template.fixedStartTm, this.template.fixedEndTm)
      }
      return false
    }
  },
  methods: {
    // 数据请求
    async queryData (params = {}) {
      this.loading = true;
      if (JSON.stringify(this.template === "{}")) {
        const resp = await getTemplate();
        this.template = resp.data.data.records.pop();
      }
      let requestData = { ...this.params, ...params };
      if (requestData.authorizationStatus == '') {
        delete requestData.authorizationStatus
      }
      try {
        const res = await fetchList(requestData);
        if (res.data.code === 0) {
          const { current, size, total, records } = res.data.data;
          this.loading = false;
          const date = new Date();
          const date1 = new Date();
          let date2
          if (this.template.conditionTm != null) {
            date2 = new Date(
              date1.getFullYear(),
              this.annualDate[0].value,
              this.annualDate[1].value
            );
          } else if (this.template.fixedStartTm && this.template.fixedEndTm) {
            date2 = this.template.fixedEndTm;
          }
          this.params = requestData;
          this.pager = { total, current, size };
          this.tableData = records.map(item => {
            item.overdueTm = this.getDaysBetween(date2, date1);
            return item
          })
        }
      } catch (error) {
        this.loading = false;
      }
    },
    toEditData (item) {
      this.$refs.editAnnual.show(item);
    },
    // 获取两个日子的间隔
    getDaysBetween (dateString1, dateString2) {
      let startDate = moment(dateString1)
      let endDate = moment(dateString2)
      return endDate.diff(startDate, "days");
    },
    resetList () {
      this.queryData({ current: 1, unitName: '', authorizationStatus: '' })
    },
    // 数据格式化
    dataFormatter (row, column, callValue) {
      switch (column.property) {
        //年审状态
        case "annualAuditStatus":
          // 返回的数据可能会是null，为null时修改成1
          let value = callValue || '1'
          let type = this.annualAuditStatus.find(
            item => item.value == value
          );
          return type ? type.label : "";
      }
    },
  }
};

</script>


